﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Read.API.Application.Command.MessageCommand;
using D.Medicalcare.Read.API.Application.Command.NavigationintheHospitalCommand;
using D.Medicalcare.Read.API.Application.Handler.MessageHandler;
using MediatR;

namespace D.Medicalcare.Read.API.Application.Handler.NavigationintheHospitalHandler
{
    /// <summary>
    /// 医院导航处理程序
    /// </summary>
    public class NavigationintheHospitalHandler : IRequestHandler<GetNavigationintheHospitalCommand, APIResult<List<NavigationintheHospital>>>
    {
        private readonly IBaseRepository<NavigationintheHospital> baseRepository;
        private readonly ILogger<NavigationintheHospitalHandler> logger;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="baseRepository">仓储</param>
        /// <param name="logger">日志</param>
        public NavigationintheHospitalHandler(IBaseRepository<NavigationintheHospital> baseRepository, ILogger<NavigationintheHospitalHandler> logger)
        {
            this.baseRepository = baseRepository;
            this.logger = logger;
        }
        /// <summary>
        /// 处理请求
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">参数</param>
        /// <returns></returns>

        public async Task<APIResult<List<NavigationintheHospital>>> Handle(GetNavigationintheHospitalCommand request, CancellationToken cancellationToken)
        {
            try
            {
                APIResult<List<NavigationintheHospital>> result = new APIResult<List<NavigationintheHospital>>();

                var data = baseRepository.GetAll().Where(x => x.IsDelete == false).ToList();
                result.Code = APIEnum.Mvc;
                result.Msg = "查询成功";

                result.Data = data;
                logger.LogInformation(result.Msg);

                return await Task.FromResult(result);
            }
            catch (Exception ex)
            {
                logger.LogError("查询异常" + ex.Message);

                throw;
            }
        }
    }
}
